var expiredTime = $("#expiredTime"),
    availableTime = $("#availableTime"),
    couponForm = $("#couponForm"),
    save_coupon = $("#save_coupon");

var couponPackId = $("input[name=id]").val();

/**
 *  初始化日历
 */
function initDatePicker() {
    availableTime.datetimepicker({
        prevText: '<i class="fa fa-chevron-left"></i>',
        nextText: '<i class="fa fa-chevron-right"></i>',
        showButtonPanel: false
    });
    // 设置默认日期
    availableTime.datetimepicker('setDate', new Date());

    expiredTime.datetimepicker({
        prevText: '<i class="fa fa-chevron-left"></i>',
        nextText: '<i class="fa fa-chevron-right"></i>',
        showButtonPanel: false
    });
    // 设置默认日期
    expiredTime.datetimepicker('setDate', new Date());
}

initDatePicker();

function initCouponPack() {
    if (couponPackId && couponPackId !== '') {
        $.get("coupon/getCouponPackById/" + couponPackId, function (data) {
            if (data) {
                $("#name").val(data.name);
                $("#bonus").val(data.bonus);
                $("#minInvestAmount").val(data.minInvestAmount);
                $("#expiredDays").val(data.expiredDays);
                availableTime.datepicker('setDate', new Date(data.availableTime));
                expiredTime.datepicker('setDate', new Date(data.expiredTime));
                $("#eventType").val(data.eventType);
            }
        });
    }
}
initCouponPack();

// 保存文章
save_coupon.on("click", function () {
    var data = couponForm.formToJson();
    //data.publishDate = publishDate.datepicker('getDate');
    $.post("coupon/saveCouponPack", data, function (res) {
        alert(res.message);
        if (res.success) {
            location.href = "coupon/list";
        }
    }).fail(function () {
        alert("服务器错误");
    });
});
